<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Websocket</title>
    <script>
        let socket = null;

        function connect() {
            // socket = new WebSocket('ws://localhost/websocket');
            socket = new WebSocket('ws://192.168.1.179/websocket');

            socket.onopen = () => {
                console.log("连接已建立");
                socket.send("Hello  Server!");
            };

            socket.onmessage = (event) => {
                console.log("收到服务端发来的消息:", event.data);
            };

            socket.onclose = () => console.log("连接已关闭");
        }

        function sendMessage() {
            const msg = document.getElementById("message").value;
            socket.send(msg);
        }
    </script>
</head>
<body>
<button onclick="connect()">连接</button>
<input id="message" placeholder="输入消息">
<button onclick="sendMessage()">发送</button>
</body>
</html>